<template>
  <div class="login">
    <el-form ref="form" :model="form" :rules="rules">
      <img class="logo" src="@/assets/common/login-logo.png" alt="">
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          prefix-icon="el-icon-lock"
          show-password
          @keyup.enter.native="submit"
          

        />
        <!-- 组件标签使用原生标签事件名.native="" -->
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="submit">登陆</el-button>
      </el-form-item>
      <div class="tip">还没有帐号？立即注册{{ $store.state.user.token }}</div>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确手机号',
            trigger: 'change'
          }
        ],
        password: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          { min: 6, max: 12, message: '请输入6-12密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        console.log('验证结果', result)
       await this.$store.dispatch('user/toLogin', this.form)
       this.$message.success('登录成功')
       const redirect=this.$route.query.redirect||'/'
        this.$router.push(redirect)
        

      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  background-image: url(~@/assets/common/login.jpg);
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .logo {
    width: 100%;
    margin-bottom: 20px;
  }
  .btn {
    width: 100%;
    background-color: #407ffe;
    color: #fff;
    border: 1px solid #3c7af8;
  }
  .tip {
    text-align: center;
    color: #fff;
    padding-top: 15px;
  }
}
</style>
